//
//  BINcollectionviewcontroller.m
//  BIN彩票
//
//  Created by mac on 15/12/28.
//  Copyright © 2015年 BINBINLULU. All rights reserved.
//

#import "BINcollectionviewcontroller.h"
#import "BINCollectionCell.h"

#import "UIView+CZFrame.h"

#import "YSMainTabBarViewController.h"



#import "LoadVcManager.h"

#import "YSLoginViewController.h"

@interface BINcollectionviewcontroller ()
@property(nonatomic,strong)UIImageView* guideSmallImageView;

@property(nonatomic,strong)UIImageView* guideLargeImageView;

@property(nonatomic,strong)UIImageView* guideImageView;

@end

@implementation BINcollectionviewcontroller

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.collectionView registerClass:[BINCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.pagingEnabled=YES;
    
    self.collectionView.bounces=NO;
    
    self.collectionView.showsHorizontalScrollIndicator=NO;
    
    NSString * guideImageName = [NSString stringWithFormat:@"guide1"];
    UIImage * guideImage = [UIImage imageNamed:guideImageName];
    UIImageView * guideImageView = [[UIImageView alloc] initWithImage:guideImage];
    self.guideImageView=guideImageView;

    
    
    NSString * guideLargeImageName = [NSString stringWithFormat:@"guideLargeText1"];
    UIImage * guideLargeImage = [UIImage imageNamed:guideLargeImageName];
    UIImageView * guideLargeImageView = [[UIImageView alloc] initWithImage:guideLargeImage];
    self.guideLargeImageView=guideLargeImageView;
    
    
    
    
    
    NSString * guideSmallImageName = [NSString stringWithFormat:@"guideSmallText1"];
    UIImage * guideSmallImage = [UIImage imageNamed:guideSmallImageName];
    UIImageView * guideSmallImageView = [[UIImageView alloc] initWithImage:guideSmallImage];
    self.guideSmallImageView=guideSmallImageView;
    
    // 创建一个按钮
    UIButton* startbtn=[[UIButton alloc] init];
    
    [startbtn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState: UIControlStateNormal];
    
    [startbtn sizeToFit];
    
    [self.collectionView addSubview:startbtn];
    
    startbtn.x=[UIScreen mainScreen].bounds.size.width*3+([UIScreen mainScreen].bounds.size.width-startbtn.w)/2;
    
    startbtn.y=[UIScreen mainScreen].bounds.size.height*0.9;
    
    [startbtn addTarget:self action:@selector(startbtnclick) forControlEvents:UIControlEventTouchUpInside];
    
    // 创建背景图片
    UIImageView* imageview=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    
    [self.collectionView addSubview:imageview];
    
    imageview.x=-202;
    
    
    
    //添加
    [self.collectionView addSubview:guideSmallImageView];
    //添加
    [self.collectionView addSubview:guideLargeImageView];
    //添加
    [self.collectionView addSubview:guideImageView];
    
    
    //修改位置
    guideLargeImageView.y = [UIScreen mainScreen].bounds.size.height * 0.75;
    guideSmallImageView.y = [UIScreen mainScreen].bounds.size.height * 0.85;
}

// 实现滚动减速结束的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 获取偏移量,根据偏移量计算图片名
    CGFloat offsetX=self.collectionView.contentOffset.x;
    
//    NSLog(@"%@",@(offsetX));
    
    //比较偏移量和图片的x坐标,来判断方向
    if (offsetX>self.guideImageView.x) {
        //人的手向左滑动,让图片从右边出来
        self.guideImageView.x=offsetX+[UIScreen mainScreen].bounds.size.width;
        self.guideLargeImageView.x=offsetX+[UIScreen mainScreen].bounds.size.width;
        self.guideSmallImageView.x=offsetX+[UIScreen mainScreen].bounds.size.width;
    }else{
        // 人得手向右滑动,让图片从左边出来
        self.guideImageView.x=offsetX-[UIScreen mainScreen].bounds.size.width;
        self.guideLargeImageView.x=offsetX-[UIScreen mainScreen].bounds.size.width;
        self.guideSmallImageView.x=offsetX-[UIScreen mainScreen].bounds.size.width;
    }
    
    
    
    // 计算是第几张照片
    int imagenum=offsetX/[UIScreen mainScreen].bounds.size.width;
    
    self.guideImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"guide%d",imagenum+1]];
    
    self.guideLargeImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"guideLargeText%d",imagenum+1]];
    
    self.guideSmallImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"guideSmallText%d",imagenum+1]];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.guideSmallImageView.x=offsetX;
        
        self.guideLargeImageView.x=offsetX;
        
        self.guideImageView.x=offsetX;
        
    }];
  
}

- (instancetype)init{
    return [super initWithCollectionViewLayout:[self loadlayout]];
    
}

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
    
    return [super initWithCollectionViewLayout:[self loadlayout]];
    
}

-(UICollectionViewLayout*)loadlayout{
    
    UICollectionViewFlowLayout* layout=[[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize=[UIScreen mainScreen].bounds.size;
    
    layout.minimumLineSpacing=0;
    
    layout.minimumInteritemSpacing=0;
    
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    return layout;
}

// 实现立即体验按钮俄方法
-(void)startbtnclick{
    // 获取当前应用程序对象
    UIApplication* app= [UIApplication sharedApplication];
    
    app.keyWindow.rootViewController= [LoadVcManager sbVCWithsbname:@"YSLoginViewController"];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BINCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor=[UIColor colorWithRed:arc4random_uniform(256)/155.f green:arc4random_uniform(256)/155.f blue:arc4random_uniform(256)/155.f alpha:1];
    
    NSString* imagename=[NSString stringWithFormat:@"guide%@Background",@(indexPath.row + 1)];
    
    UIImage* image=[UIImage imageNamed:imagename];
    
    cell.image=image;

    return cell;
}





@end
